package com.whub507.msgmanager.security.service;

import com.whub507.msgmanager.security.domain.User;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserService {
    private final List<User> online_user_list=new ArrayList<>();

    public Boolean isOnline(String username){
        return getUserByName(username)==null?Boolean.FALSE:Boolean.TRUE;
    }

    public User getUserByName(String username){
        if (username==null)
            return null;
        List<User> users= online_user_list.stream().filter(user -> user.getUserName().equals(username)).collect(Collectors.toList());
        if(users.size()==0){
            return null;
        }else {
            return users.get(0);
        }
    }

    public User login(User user){
        user.setState(1);
        this.online_user_list.add(user);
        return user;
    }

    public Boolean logout(String username){
        online_user_list.remove(getUserByName(username));
        return Boolean.TRUE;
    }
}
